home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / libsrc / c / io / fseek.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  2.0 KB  |  90 lines

  1. /* This is file FSEEK.C */
  2. /* This file may have been modified by DJ Delorie (Jan 1991).  If so,
  3. ** these modifications are Coyright (C) 1993 DJ Delorie, 24 Kirsten Ave,
  4. ** Rochester NH, 03867-2954, USA.
  5. */
  6.  
  7. /* This file was modified 1991-10-20 by R. Davidson in order to correct
  8. ** for ascii text files.
  9. */
  10.  
  11. #if defined(LIBC_SCCS) && !defined(lint)
  12. static char sccsid[] = "@(#)fseek.c    5.3 (Berkeley) 3/9/86";
  13. #endif LIBC_SCCS and not lint
  14.  
  15. /*
  16.  * Seek for standard library.  Coordinates with buffering.
  17.  */
  18.  
  19. #include    <stdio.h>
  20.  
  21. long lseek();
  22.  
  23. fseek(iop, offset, ptrname)
  24.     register FILE *iop;
  25.     long offset;
  26. {
  27.     register resync, c;
  28.     long p = -1;            /* can't happen? */
  29.     long adjust = 0;
  30.     char *q, *qq;
  31.  
  32.     iop->_flag &= ~_IOEOF;
  33.     if (iop->_flag&_IOREAD) {
  34. #if 0
  35.         if (ptrname<2 && iop->_base &&
  36.             !(iop->_flag&_IONBF)) {
  37.             c = iop->_cnt;
  38.             p = offset;
  39.             if (ptrname==0) {
  40.                 long curpos = lseek(fileno(iop), 0L, 1);
  41.                 if (curpos == -1)
  42.                     return (-1);
  43.                 p += c - curpos;
  44.             } else
  45.                 offset -= c;
  46.             if(!(iop->_flag&_IORW) && c>0&&p<=c
  47.                 && p>=iop->_base-iop->_ptr){
  48.                 if (iop->_flag&_IOTEXT){
  49.                   q = &iop->_ptr[iop->_cnt];
  50.                   qq = iop->_ptr + (int)p;
  51.                   while (--q >= qq)
  52.                     if (*q == '\n'){
  53.                       adjust++; qq++;
  54.                     }
  55.                   p += adjust;
  56.                 }
  57.                 iop->_ptr += (int)p;
  58.                 iop->_cnt -= (int)p;
  59.                 return(0);
  60.             }
  61.             resync = offset&01;
  62.         } else
  63.             resync = 0;
  64. #endif
  65.         if (iop->_flag & _IORW) {
  66.             iop->_ptr = iop->_base;
  67.             iop->_flag &= ~_IOREAD;
  68.             resync = 0;
  69.         }
  70.         p = lseek(fileno(iop), offset-resync, ptrname);
  71.         iop->_cnt = 0;
  72. #if 0
  73.         if (resync && p != -1)
  74.             if (getc(iop) == EOF)
  75.                 p = -1;
  76. #endif
  77.     }
  78.     else if (iop->_flag & (_IOWRT|_IORW)) {
  79.         p = fflush(iop);
  80.         if (iop->_flag & _IORW) {
  81.             iop->_cnt = 0;
  82.             iop->_flag &= ~_IOWRT;
  83.             iop->_ptr = iop->_base;
  84.         }
  85.         return(lseek(fileno(iop), offset, ptrname) == -1 || p == EOF ?
  86.             -1 : 0);
  87.     }
  88.     return(p==-1?-1:0);
  89. }
  90.